﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using MySQLStudio.DataAccess;
using MySQLStudio.Common;

namespace MySQLStudio.BusinessLogic
{
    public class StoredProcedure : BaseDatabaseObject
    {
        private DateTime m_created;
        private DateTime? m_lastAltered;
        private string m_definer;               

        public DateTime Created
        {
            get { return m_created; }
            set { m_created = value; }
        }

        public DateTime? LastAltered
        {
            get { return m_lastAltered; }
            set { m_lastAltered = value; }
        }

        public string Definer
        {
            get { return m_definer; }
            set { m_definer = value; }
        }

        internal StoredProcedure(string name, string schema, string type, DateTime created, DateTime? lastAltered, string definer)
        {
            this.m_name = name;
            this.m_schema = schema;
            this.m_type = type;
            this.m_created = created;
            this.m_lastAltered = lastAltered;
            this.m_definer = definer;
        }


        public string GetCreateStatement(string connectionString)
        {
            IDataReader reader = null;
            string createProcedureStatement = null;

            try
            {
                reader = MySqlDataAccess.ExecuteDataReader(connectionString, InformationSchemaHelper.GetStoredProcedureCreateStatementCommand(this.m_name, this.m_schema));

                if (reader.Read())
                    createProcedureStatement = Convert.ToString(reader["Create Procedure"]);                
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ErrorMessages.GetString(ErrorMessages.InformationSchema_Failed_to_generate_stored_procedure_create_statement, this.m_name), ex);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }

            return createProcedureStatement;
        }

        public string GetAlterStatement(string connectionString)
        {
            IDataReader reader = null;
            string alterProcedureStatement = string.Empty;

            try
            {
                reader = MySqlDataAccess.ExecuteDataReader(connectionString, InformationSchemaHelper.GetStoreProcedureAlterStatementCommand(this.m_name, this.m_schema));

                if (reader.Read())
                    alterProcedureStatement = Convert.ToString(reader["Create Procedure"]);

            }
            catch (Exception ex)
            {
                throw new ApplicationException(ErrorMessages.GetString(ErrorMessages.InformationSchema_Failed_to_generate_stored_procedure_alter_statement, this.m_schema, this.m_name), ex);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }

            return string.Format("DROP PROCEDURE IF EXISTS {0};\n{1}", this.m_name, alterProcedureStatement);
        }

        public void Delete(string connectionString)
        {
            int errCode = 0;
            try
            {
                errCode = MySqlDataAccess.ExecuteNonQuery(connectionString, InformationSchemaHelper.GetDeleteProcedureStatementCommand(this.m_name, this.m_schema));
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ErrorMessages.GetString(ErrorMessages.InformationSchema_Failed_to_delete_stored_procedure, this.m_schema, this.m_name), ex);
            }                      
        }
    }
}
